package com.fj.bio;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author fj
 * @date 2023/12/6 19:27
 */
public class BIOServer {
    public static void main(String[] args) throws IOException {
        ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
        ServerSocket serverSocket = new ServerSocket(6666);
        System.out.println("服务器启动成功");
        while (true){
            final Socket accept = serverSocket.accept();
            System.out.println("客户端接入");
            cachedThreadPool.execute(() -> {
                try(InputStream inputStream = accept.getInputStream();){
                    byte[] bytes = new byte[1024];
                    while (true){
                        int read = inputStream.read(bytes);
                        if (read != -1){
                            String line = new String(bytes, 0, read);
                            System.out.println(line);
                        }else{
                            break;
                        }
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            });
        }
    }
}
